/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import ezcms.exceptions.PresentationException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import util.UploadUtil;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class ResourceHandler extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest hsr;
    private InputStream inputStream;
    private String filename;
    private String contentDisposition;

    @Override
    public String execute() throws PresentationException {
        if (filename == null) {
            return ERROR;
        } else {
            //TODO: Check permission
            System.out.println("Serving file: " + getFilename());
            try {
                setInputStream(UploadUtil.getFile(hsr.getServletContext(), getFilename()));
            } catch (Exception ex) {
                System.out.println("ERRRRRRR");
                throw new PresentationException(ex);
            }
            return SUCCESS;
        }
    }

    /**
     * @return the inputStream
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * @param inputStream the inputStream to set
     */
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * @return the filename
     */
    public String getFilename() {
        return filename;
    }

    /**
     * @param filename the filename to set
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }

    /**
     * @return the contentDisposition
     */
    public String getContentDisposition() {
        return contentDisposition;
    }

    /**
     * @param contentDisposition the contentDisposition to set
     */
    public void setContentDisposition(String contentDisposition) {
        this.contentDisposition = contentDisposition;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.hsr = hsr;
    }
}
